/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entitydao;

import database.Database;
import entity.Nakladatelstvi;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Lattis
 */
public class NakladatelstviDAO extends TableDAO implements DAO<Nakladatelstvi> {

    private static final String TABLE_NAME = "LIB_NAKLADATELSTVI";
    private static final String FIND_ALL = "SELECT * FROM LIB_NAKLADATELSTVI ORDER BY ID_NAKLADATELSTVI ASC";
    private static final String FIND_BY_ID = "SELECT * FROM " + TABLE_NAME + " WHERE ID_NAKLADATELSTVI = ?";
    private static final String FIND_BY_NAZEV = "SELECT * FROM " + TABLE_NAME + " WHERE NAZEV = ?";
    private static final String INSERT = "INSERT INTO LIB_NAKLADATELSTVI(NAZEV,MESTO) VALUES(?,?)";
    private static final String DELETE = "DELETE FROM LIB_NAKLADATELSTVI WHERE ID_NAKLADATELSTVI = ?";
    private static final String FIND_BY_MESTO = "SELECT * FROM " + TABLE_NAME + " WHERE MESTO = ?";
    Connection mConnection;

    public NakladatelstviDAO(Database database) {
        super(database, TABLE_NAME);
        mConnection = database.Connection();
    }

    public List<Nakladatelstvi> findAll() {
        List<Nakladatelstvi> listNakladatelstvi = new ArrayList<Nakladatelstvi>();
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try {
            pstm = mConnection.prepareStatement(FIND_ALL);
            rs = pstm.executeQuery();
            listNakladatelstvi = read(rs);
        } catch (SQLException e) {
            mDatabase.Rollback();
            listNakladatelstvi = null;
        } finally {
            mDatabase.EndTransaction();
            return listNakladatelstvi;
        }
    }

    public List<Nakladatelstvi> findByNazev(String nazev) {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        List<Nakladatelstvi> nakladatelstviList = new ArrayList<Nakladatelstvi>();
        try {
            pstm = mConnection.prepareStatement(FIND_BY_NAZEV);
            pstm.setString(1, nazev);
            rs = pstm.executeQuery();
            while (rs.next()) {
                nakladatelstviList.add(new Nakladatelstvi(rs.getInt(1), rs.getString(2), rs.getString(3)));
            }
        } catch (SQLException e) {
            System.err.println(e.getMessage());
            nakladatelstviList = null;
        } finally {
            close(rs, pstm);
            return nakladatelstviList;
        }
    }

    public List<Nakladatelstvi> findByMesto(String mesto) {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        List<Nakladatelstvi> nakladatelstviList = new ArrayList<Nakladatelstvi>();
        try {
            pstm = mConnection.prepareStatement(FIND_BY_MESTO);
            pstm.setString(1, mesto);
            rs = pstm.executeQuery();
            while (rs.next()) {
                nakladatelstviList.add(new Nakladatelstvi(rs.getInt(1), rs.getString(2), rs.getString(3)));
            }
        } catch (SQLException e) {
            System.err.println(e.getMessage());
            nakladatelstviList = null;
        } finally {
            close(rs, pstm);
            return nakladatelstviList;
        }
    }

    public Nakladatelstvi findById(int id) {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        Nakladatelstvi naklad = null;
        try {
            pstm = mConnection.prepareStatement(FIND_BY_ID);
            pstm.setInt(1, id);
            rs = pstm.executeQuery();
            while (rs.next()) {
                naklad = new Nakladatelstvi(rs.getInt(1), rs.getString(2), rs.getString(3));
            }
        } catch (SQLException e) {
            System.err.println(e.getMessage());
            naklad = null;
        } finally {
            close(rs, pstm);
            return naklad;
        }
    }

    public boolean insert(Nakladatelstvi entity) {
        PreparedStatement pstm = null;
        boolean ret = false;
        int rownum = 0;
        try {
            pstm = mConnection.prepareStatement(INSERT);
            pstm.setString(1, entity.getNazev());
            pstm.setString(2, entity.getMesto());
            mDatabase.BeginTransaction();
            mConnection.setTransactionIsolation(mConnection.TRANSACTION_READ_COMMITTED);
            rownum = pstm.executeUpdate();
            if (rownum != 0) {
                ret = false;
            }
        } catch (SQLException e) {
            mDatabase.Rollback();
//            System.err.println("Nakladatelstvi ROLLBACK > " + e.getMessage());
            ret = false;
        } finally {
            mDatabase.EndTransaction();
            close(null, pstm);
            return ret;
        }
    }

    public boolean delete(Nakladatelstvi entity) {
        PreparedStatement pstm = null;
        boolean ret = false;
        try {
            pstm = mConnection.prepareStatement(DELETE);
            pstm.setInt(1, entity.getId_nakladatelstvi());
            mDatabase.BeginTransaction();
            if (!pstm.execute()) {
                ret = true;
            }
        } catch (SQLException ex) {
            mDatabase.Rollback();
            System.err.println("Nakladatelstvi ROLLBACK > " + ex.getMessage());
            ret = false;
        } finally {
            mDatabase.EndTransaction();
            close(null, pstm);
            return ret;
        }
    }

    public boolean update(Nakladatelstvi entity) {
        throw new UnsupportedOperationException("Not supported yet.");
//        mConnection.setTransactionIsolation(mConnection.TRANSACTION_READ_COMMITTED);
    }

    public List<Nakladatelstvi> read(ResultSet rs) throws SQLException {
        List<Nakladatelstvi> listNakladatelstvi = new ArrayList<Nakladatelstvi>();
        while (rs.next()) {
            Nakladatelstvi n = new Nakladatelstvi();
            n.setId_nakladatelstvi(rs.getInt(1));
            n.setNazev(rs.getString(2));
            n.setMesto(rs.getString(3));
            listNakladatelstvi.add(n);
        }
        return listNakladatelstvi;
    }

    public void close(ResultSet rs, PreparedStatement pstm) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (pstm != null) {
                pstm.close();
            }
        } catch (Exception e) {
            System.err.println("Closing Error");
        }
    }
}
